<template>
  <Layout>
    <el-card
      class="blog"
      v-for="(blog, i) in blogs"
      :key="blog.id"
      shadow="hover"
    >
      <div slot="header">
        <el-row>
          <el-col :span="16">
            <span>
              <g-link
                style="text-decoration: none; cursor: pointer"
                :to="'/gist/' + blog.id"
              >
                <i class="el-icon-edit-outline"></i>&nbsp;&nbsp;
                {{ blogTitles[i] }}
              </g-link>
            </span>
          </el-col>
          <el-col :span="8">
            <div style="text-align: right">
              <el-button
                @click="share(i)"
                style="padding: 3px 0"
                type="text"
                icon="el-icon-share"
              ></el-button>
              <el-button
                @click="editBlog(i)"
                style="padding: 3px 0"
                type="text"
                icon="el-icon-edit"
                v-if="token"
              ></el-button>
              <el-button
                @click="deleteBlog(i)"
                style="padding: 3px 0"
                type="text"
                icon="el-icon-delete"
                v-if="token"
              ></el-button>
            </div>
          </el-col>
        </el-row>
      </div>

      <div style="font-size: 0.9rem; line-height: 1.5; color: #606c71">
        最近更新 {{ $util.utcToLocal(blog.updated_at) }}
      </div>
      <div
        style="
          font-size: 1.1rem;
          line-height: 1.5;
          color: #303133;
          padding: 10px 0px 0px 0px;
        "
      >
        {{ blog.description }}
      </div>
    </el-card>
    <div style="text-align: center">
      <el-pagination
        @current-change="changePage"
        background
        layout="prev, pager, next"
        :current-page.sync="pageInfo.currentPage"
        :page-size="5"
        :total="pageInfo.count"
        hide-on-single-page
      >
      </el-pagination>
    </div>
  </Layout>
</template>

<page-query>
query($page: Int) {
  allGists (perPage: 5, page: $page) @paginate {
    totalCount
    pageInfo {
      totalPages
      currentPage
    }
    edges {
      node {
        id
        description
        updated_at
        files
        html_url
      }
    }
  }
}
</page-query>

<script>
import { Pager } from "gridsome";
export default {
  metaInfo: {
    title: "博客列表",
  },
  components: {
    Pager,
  },
  computed: {
    blogs() {
      let blogs = [];
      this.$page.allGists.edges.forEach((x) => blogs.push(x.node));
      return blogs;
    },
    blogTitles() {
      let titles = [];
      this.blogs.forEach((x) =>
        titles.push(Object.keys(JSON.parse(x.files))[0])
      );
      return titles;
    },
    pageInfo() {
      let pageInfo = {};
      pageInfo.count = this.$page.allGists.totalCount;
      pageInfo.currentPage = this.$page.allGists.currentPage;
      return pageInfo;
    },
    token() {
      return null;
    },
  },
  methods: {
    share(i) {
      this.$util.copy(this.blogs[i].html_url);
      this.$message({
        message: "链接已复制,去分享给好友吧!!",
        type: "success",
      });
    },
    editBlog(i) {},
    deleteBlog(i) {},
    changePage(i) {
      if (i > 1) this.$router.push("/blog/" + i).catch(() => {});
      else this.$router.push("/blog").catch(() => {});
    },
  },
};
</script>

<style scoped>
a {
  color: #1e6bb8;
}
.blog {
  margin-bottom: 20px;
}
</style>
